package com.gucheng.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gucheng.common.ResponseResult;
import com.gucheng.constants.SystemConstants;
import com.gucheng.exception.SystemException;
import com.gucheng.mapper.CommentMapper;
import com.gucheng.model.entity.Comment;
import com.gucheng.model.enums.AppHttpCodeEnum;
import com.gucheng.model.vo.CommentVo;
import com.gucheng.model.vo.PageVo;
import com.gucheng.service.CommentService;
import com.gucheng.service.UserService;
import com.gucheng.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2024-09-03 07:41:49
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
   @Autowired
   private UserService userService;

   @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        // 查询对应文章的根评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        // 对articleId进行判断
        queryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId);
        // 根评论rootId为-1
        queryWrapper.eq(Comment::getRootId,-1);

        // 评论类型
       queryWrapper.eq(Comment::getType,commentType);
        // 分页查询
        Page<Comment> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);

        List<CommentVo> commentVoList = toCommentVoList(page.getRecords());

        // 查询所有根评论对应的子评论集合，并且赋值给对应的属性
        commentVoList.stream()
                .map(commentVo -> {
                    List<CommentVo> children = getChildren(commentVo.getId());
                    commentVo.setChildren(children);
                    return commentVo;
                })
                .collect(Collectors.toList());

        return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }

    @Override
    public ResponseResult addComment(Comment comment) {
       // 评论内容不能为空
        if (!StringUtils.hasText(comment.getContent())){
            throw  new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
       save(comment);
       return ResponseResult.okResult();
    }



    /**
     *
     * @param id 根评论id查询对应子评论的集合
     * @return
     */
    private List<CommentVo> getChildren(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByDesc(Comment::getCreateTime);
        List<Comment> comments = list(queryWrapper);

        List<CommentVo> commentVos = toCommentVoList(comments);

        return commentVos;
    }

    private List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        return commentVos.stream()
                .peek(commentVo -> {
                    String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
                    commentVo.setUsername(nickName);
                })
                .map(commentVo -> {
                    if (commentVo.getToCommentUserId()!= -1) {
                        String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                        commentVo.setToCommentUserName(toCommentUserName);
                    }
                    return commentVo;
                })
                .collect(Collectors.toList());
    }
}
